package com.sdhs.paas.codegenerate.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdhs.paas.codegenerate.bean.Attribute;
import com.sdhs.paas.codegenerate.bean.Column;
import com.sdhs.paas.codegenerate.utils.FreeMarkerTemplateUtils;
import com.sdhs.paas.codegenerate.utils.Utils;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class GenerateCodeService {

	/**
	 * 实体生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateEntity(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String className = attrs.get("className")+"Bean";
		map.put("entityPackageName", attrs.get("packageName")+".bean");
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("attrs", attrs.get("attrs"));
		map.put("columns",attrs.get("columns"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"\\bean\\";
		final String templateName = "Entity.ftl";
		final String suffix = ".java";
		final String path = realPath + className + suffix;
		generateFileByTemplate(templateName, map, path);
		return realPath + "." + attrs.get("className");
	}
	
	/**
	 * DTO实体生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateDTO(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String className = attrs.get("className")+"DTO";
		map.put("entityPackageName", attrs.get("packageName")+".dto");
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("attrs", attrs.get("attrs"));
		map.put("columns",attrs.get("columns"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"\\dto\\";
		final String templateName = "DTO.ftl";
		final String suffix = ".java";
		final String path = realPath + className + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成dto代码生成功";
	}
	
	/**
	 * resDTO实体生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateResDTO(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String nativeClassName = String.valueOf(attrs.get("className"));
		String classType = nativeClassName+"DTO";
		String className = attrs.get("className")+"ResDTO";
		List<Attribute> attr_list = new ArrayList<Attribute>();
		Attribute dtoObj = new Attribute();
		dtoObj.setName(Utils.fristWordToLowerCase(nativeClassName+"DTO"));
		dtoObj.setType(classType);
		Attribute dtoListObj = new Attribute();
		dtoListObj.setName(Utils.fristWordToLowerCase(nativeClassName+"DTOList"));
		dtoListObj.setType("List<"+classType+">");
		attr_list.add(dtoObj);
		attr_list.add(dtoListObj);
		map.put("entityPackageName", attrs.get("packageName")+".dto.res");
		map.put("importClassName", attrs.get("packageName")+".dto."+classType);
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("attrs", attr_list);
		map.put("columns",attrs.get("columns"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"\\dto\\res\\";
		final String templateName = "ResDTO.ftl";
		final String suffix = ".java";
		final String path = realPath + className + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成dto代码生成功";
	}
	
	/**
	 * reqDTO实体生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateReqDTO(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String nativeClassName = String.valueOf(attrs.get("className"));
		String classType = nativeClassName+"DTO";
		String className = attrs.get("className")+"ReqDTO";
		List<Attribute> attr_list = new ArrayList<Attribute>();
		Attribute dtoObj = new Attribute();
		dtoObj.setName(Utils.fristWordToLowerCase(nativeClassName+"DTO"));
		dtoObj.setType(classType);
		Attribute dtoListObj = new Attribute();
		dtoListObj.setName(Utils.fristWordToLowerCase(nativeClassName+"DTOList"));
		dtoListObj.setType("List<"+classType+">");
		attr_list.add(dtoObj);
		attr_list.add(dtoListObj);
		map.put("entityPackageName", attrs.get("packageName")+".dto.req");
		map.put("importClassName", attrs.get("packageName")+".dto."+classType);
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("attrs", attr_list);
		map.put("columns",attrs.get("columns"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"\\dto\\req\\";
		final String templateName = "ReqDTO.ftl";
		final String suffix = ".java";
		final String path = realPath + className + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成dto代码生成功";
	}
	
	/**
	 * mapper接口代码生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateMapperInterface(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String className = String.valueOf(attrs.get("className"));
		String classBeanName = attrs.get("className")+"Bean";
		map.put("mapperPackageName", attrs.get("packageName")+".repository");
		map.put("importClassName", attrs.get("packageName")+".bean."+classBeanName);
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"\\repository\\";
		final String templateName = "Mapper.ftl";
		final String suffix = ".java";
		final String path = realPath + className+"Mapper" + suffix;
		generateFileByTemplate(templateName, map, path);
		return "mapper接口代码生成";
	}
	
	/**
	 * mapperXML代码生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateMapperXML(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String className = String.valueOf(attrs.get("className"));
		List<Column> columns =(List<Column>) attrs.get("nativecolumns");
		Column primaryKey = new Column();
		for(Column column : columns){
			if("1".equals(column.getPrimaryKey())){
				primaryKey = column;
				break;
			}
		}
		map.put("mapperXmlPackageName", attrs.get("packageName"));
		map.put("className", className);
		map.put("columns", columns);
		map.put("primaryKey", primaryKey);
		map.put("tableName", attrs.get("tableName"));
		String realPath = String.valueOf(attrs.get("xmlPath"));
		final String templateName = "MapperXML.ftl";
		final String suffix = ".xml";
		final String path = realPath + className+"Mapper" + suffix;
		generateFileByTemplate(templateName, map, path);
		return "MapperXML代码生成";
	}
	
	/**
	 * service代码生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateService(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("servicePackageName", attrs.get("packageName"));
		String className = String.valueOf(attrs.get("className"));
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("realPath"))+"service\\";
		final String templateName = "Service.ftl";
		final String suffix = ".java";
		final String path = realPath + className+"Service" + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成service代码成功";
	}
	
	/**
	 * controller代码生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateController(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("controllerPackageName", attrs.get("packageName"));
		String className = String.valueOf(attrs.get("className"));
		String realPath = String.valueOf(attrs.get("realPath"))+"app\\";
		map.put("className", className);
		map.put("author", attrs.get("author"));
		map.put("date", attrs.get("date"));
		final String templateName = "Controller.ftl";
		final String suffix = ".java";
		final String path = realPath + className+"Controller" + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成controller代码成功";
	}
	/**
	 * 代码生成器
	 * 
	 * @param templateName
	 *            使用的模板名称
	 * @param attrs
	 *            赋值的参数
	 * @param path
	 *            生成的路径
	 */
	private static void generateFileByTemplate(String templateName, Map<String, Object> attrs, String path) {
		File file = new File(path);
		Template template = null;
		try {
			template = FreeMarkerTemplateUtils.getTemplate(templateName);
			FileOutputStream fos = new FileOutputStream(file);
			Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
			template.process(attrs, out);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
}
